#include "dlg_bookam.h"
#include "ui_dlg_bookam.h"
#include "lib/sqlmgr.h"

Dlg_AM::Dlg_AM(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dlg_AM)
{
    ui->setupUi(this);
}

Dlg_AM::~Dlg_AM()
{
    delete ui;
}

void Dlg_AM::setType(int id)
{
    m_id = id;
    //执行sql语句获取图书信息，并且显示到对应的控件上
    auto l = SqlMgr::getInstance()->getBooks(QString(" where bookid =%1").arg(id));
    if(1 == l.size())
    {
        auto data = l[0];
        ui->le_name->setText(data[1]);
        ui->le_press->setText(data[2]);
        ui->cb_type1->setCurrentText(data[3]);
        ui->cb_type2->setCurrentText(data[4]);
        ui->cb_type3->setCurrentText(data[5]);
        ui->le_cnt->setText(data[6]);
    }
}

void Dlg_AM::on_btn_yes_clicked()
{
    QStringList l;
    l << QString::number(m_id);
    l << ui->le_name->text();
    l << ui->le_press->text();
    l << ui->cb_type1->currentText();
    l << ui->cb_type2->currentText();
    l << ui->cb_type3->currentText();
    l << ui->le_cnt->text();
    l << "";
    if(m_id != -1) {
        SqlMgr::getInstance()->modifyBook(l); //更新
    }
    else
    {
        //新增
        QVector<QStringList> vec;
        vec.push_back(l);
        SqlMgr::getInstance()->addBook(vec);
    }
    this->hide(); //隐藏窗口
}

void Dlg_AM::on_btn_no_clicked()
{
    this->hide(); //隐藏窗口
}
